package com.paipai.interceptor;

import com.paipai.utils.CookieUtils;
import com.paipai.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Component
public class JwtRenewInterceptor extends HandlerInterceptorAdapter {

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handle) throws IOException {
        String redisUUId = null;
        String cookieCheckId = null;
        String cookieUsername = null;
        String account = null;
        String usernameFromRedis = null;
        Map<String, String> map = new HashMap<String,String>();


        try {
            String cookieUserToken = "";
            cookieUserToken = CookieUtils.getCookie("token", request);
            cookieCheckId = CookieUtils.getCookie("checkId",request);
            cookieUsername = CookieUtils.getCookie("account",request);

            // 从redis中拿数据
            if(!(redisTemplate.opsForValue().get("PAI"+cookieUsername) ==null)) {
                map = (Map<String, String>) redisTemplate.opsForValue().get("PAI" + cookieUsername);
                redisUUId = map.get("uuid");
                usernameFromRedis = map.get("account");
            }
            System.out.println(map);
            account = JwtUtils.getAccount(cookieUserToken);
            System.out.println("-----------"+account+"放行");


        }catch (Exception e){
            // cookie中的token过期
//            e.printStackTrace();
            // 登录的uuid一致
            if(cookieCheckId!=null){
//                Map<String, String> map = (Map<String, String>) redisTemplate.opsForValue().get("PAI" + cookieUsername);
//                redisUUId = map.get("uuid");
                System.out.println("---------------redisUUId"+redisUUId);
                System.out.println("---------------cookieCheckId"+cookieCheckId);
                if(cookieCheckId.equals(redisUUId)){
                    //当redis中的数据未过期时，进行续期
                    System.out.println("==========token过期，进行续期==============");
                    String freshToken=JwtUtils.createToken(cookieUsername,5);
                    CookieUtils.setUserToken2Cookie("token",freshToken,response);
                    CookieUtils.setUserToken2Cookie("account",cookieUsername,response);
                    CookieUtils.setUserToken2Cookie("checkId",cookieCheckId,response);

                    map.put("token",freshToken);
                    map.put("account",usernameFromRedis);
                    map.put("uuid",cookieCheckId);
                    redisTemplate.opsForValue().set("PAI" + usernameFromRedis, map, 10, TimeUnit.MINUTES);
                }else {
                    // cookie中与redis中的uuid不一致
                    CookieUtils.deleteUserTokenFromCookie( "token",response);
                    CookieUtils.deleteUserTokenFromCookie( "checkId",response);
                    CookieUtils.deleteUserTokenFromCookie( "account",response);
                    System.out.println("cookie中与redis中的uuid不一致");
                    response.sendRedirect("/toLogin");
                    return false;
                }
            }
            else {
                // cookie中没有数据
                System.out.println("cookie中没有数据");
                response.sendRedirect("/toLogin");
                return false;
            }
        }
        System.out.println("------------------------------------放行");
        return true;
    }
}
